/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.GodownEntry;
import me.zhengjie.modules.system.service.GodownEntryService;
import me.zhengjie.modules.system.service.dto.GodownEntryQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.system.service.dto.GodownEntryDto;
import org.springframework.web.multipart.MultipartFile;

/**
* @website https://eladmin.vip
* @author 李忠臻
* @date 2024-02-24
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "入库单管理")
@RequestMapping("/api/godownEntry")
public class GodownEntryController {

    private final GodownEntryService godownEntryService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('godownEntry:list')")
    public void exportGodownEntry(HttpServletResponse response, GodownEntryQueryCriteria criteria) throws IOException {
        godownEntryService.download(godownEntryService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询入库单")
    @ApiOperation("查询入库单")
//    @PreAuthorize("@el.check('godownEntry:list')")
    public ResponseEntity<PageResult<GodownEntryDto>> queryGodownEntry(GodownEntryQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(godownEntryService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增入库单")
    @ApiOperation("新增入库单")
    @PreAuthorize("@el.check('godownEntry:add')")
    public ResponseEntity<Object> createGodownEntry(@Validated @RequestBody GodownEntry resources){
        godownEntryService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改入库单")
    @ApiOperation("修改入库单")
    @PreAuthorize("@el.check('godownEntry:edit')")
    public ResponseEntity<Object> updateGodownEntry(@Validated @RequestBody GodownEntry resources){
        godownEntryService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除入库单")
    @ApiOperation("删除入库单")
    @PreAuthorize("@el.check('godownEntry:del')")
    public ResponseEntity<Object> deleteGodownEntry(@RequestBody Long[] ids) {
        godownEntryService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/pass/{id}")
    @Log("审核通过入库单主表")
    @ApiOperation("审核通过入库单主表")
    @PreAuthorize("@el.check('godownEntry:audit')")
    public ResponseEntity<Object> passPurchaseRequest(@PathVariable Long id) {
        godownEntryService.pass(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/cancel/{id}")
    @Log("审核不通过入库单主表")
    @ApiOperation("审核不通过入库单主表")
    @PreAuthorize("@el.check('godownEntry:audit')")
    public ResponseEntity<Object> cancelPurchaseRequest(@PathVariable Long id) {
        godownEntryService.cancel(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/upload")
    @ApiOperation("上传文件")
    @PreAuthorize("@el.check('godownEntry:add')")
    public ResponseEntity<Object> uploadGodownEntry(@RequestParam String name, @RequestParam("file") MultipartFile file) {
//        localStorageService.create(name, file);
//        PurchaseRequest resources = new PurchaseRequest();
        godownEntryService.upload(name, file);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
}